package com.cloud.charts4a.demo;

import java.util.ArrayList;
import java.util.List;
import com.cloud.charts4a.chart.LineChart;
import com.cloud.charts4a.chart.LineChart.IAfterDragChartListener;
import com.cloud.charts4a.chart.LineChart.XAxisAttribs;
import com.cloud.charts4a.chart.attrib.YAxisAttribs;
import com.cloud.charts4a.chart.attrib.YRangeHighLightAttribs;
import com.cloud.charts4a.data.ChartDataSeries;
import com.cloud.charts4a.data.LineChartData;
import com.cloud.charts4a.util.MathUtil;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ToggleButton;

/**
 * Line Chart Demo</p>
 * 
 * @version
 * <ol>
 * 	<li>2010/07/09 CloudTu：First Release</li>
 * </ol>  
 * 
 * @author cloudtu
 */
public class LineChartDemo extends AbstractChartDemo {	
	private LineChart lineChart;		
	private int minXValue = 3;
	private int maxXValue = 10;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.line_chart);
        
		lineChart = (LineChart)findViewById(R.id.lineChart);
        
        initLineChartView(minXValue, maxXValue);
    }    
    
	@Override
	protected Dialog createChartConfigDialog() {
    	LayoutInflater layoutInflater = LayoutInflater.from(this);
    	View chartConfigView = layoutInflater.inflate(R.layout.chart_config, null);
        
    	final ToggleButton showXUnitDescButton = (ToggleButton)chartConfigView.findViewById(R.id.showXUnitDescButton);
        showXUnitDescButton.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				lineChart.setShowXUnitDesc(showXUnitDescButton.isChecked());
			}
		});        
        
        final ToggleButton showYUnitDescButton = (ToggleButton)chartConfigView.findViewById(R.id.showYUnitDescButton);
        showYUnitDescButton.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				lineChart.setShowYUnitDesc(showYUnitDescButton.isChecked());
			}
		});        
        
        final ToggleButton showLegendButton = (ToggleButton)chartConfigView.findViewById(R.id.showLegendButton);
        showLegendButton.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				lineChart.setShowLegend(showLegendButton.isChecked());
			}
		});   
        
        final ToggleButton onlyShowDotButton = (ToggleButton)chartConfigView.findViewById(R.id.onlyShowDotButton);
        onlyShowDotButton.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				lineChart.setOnlyShowDot(onlyShowDotButton.isChecked());
			}
		});          
        
        final ToggleButton fixedYAxisRangeButton = (ToggleButton)chartConfigView.findViewById(R.id.fixedYAxisRangeButton);
        fixedYAxisRangeButton.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				if(fixedYAxisRangeButton.isChecked()){
			    	XAxisAttribs xAxisAttribs = new XAxisAttribs(minXValue,maxXValue,2,getString(R.string.group));     
			    	YAxisAttribs yAxisAttribs = new YAxisAttribs(0,150,30, getString(R.string.amount));		
					lineChart.initChartBackground(xAxisAttribs, yAxisAttribs);				
				}
				else{
			    	XAxisAttribs xAxisAttribs = new XAxisAttribs(minXValue,maxXValue,2,getString(R.string.group));     
			    	YAxisAttribs yAxisAttribs = new YAxisAttribs(20, getString(R.string.amount));		
					lineChart.initChartBackground(xAxisAttribs, yAxisAttribs);							
				}				
			}
		});         
        
        final ToggleButton setYRangeHighLightAttribsButton = (ToggleButton)chartConfigView.findViewById(R.id.setYRangeHighLightAttribsButton);
        setYRangeHighLightAttribsButton.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				if(setYRangeHighLightAttribsButton.isChecked()){
					lineChart.setYRangeHighLightAttribs(new YRangeHighLightAttribs(80,110,Color.argb(128, 153, 102, 51)));							
				}
				else{
					lineChart.setYRangeHighLightAttribs(null);							
				}
			}
		});  
        
        final ToggleButton setAfterDragChartListenerButton = (ToggleButton)chartConfigView.findViewById(R.id.setAfterDragChartListenerButton);
        setAfterDragChartListenerButton.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				if(setAfterDragChartListenerButton.isChecked()){
					lineChart.setAfterDragChartListener(new IAfterDragChartListener() {
						@Override
						public List<ChartDataSeries<LineChartData>> reCreateChartContent(int minXValue, int maxXValue) {
							return createMockLineChartData(minXValue,maxXValue);
						}	
					});					
				}
				else{
					lineChart.setAfterDragChartListener(null);
				}
			}
		});        
        
    	return new AlertDialog.Builder(LineChartDemo.this)
			.setTitle(R.string.chartConfig)
			.setView(chartConfigView)
			.setPositiveButton(R.string.ok,new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog,int whichButton) {			        
			        lineChart.drawChart();
				}
			})
			.create();
	}
    
    private void initLineChartView(int minXValue, int maxXValue){		
    	XAxisAttribs xAxisAttribs = new XAxisAttribs(minXValue,maxXValue,2,getString(R.string.group));     
    	YAxisAttribs yAxisAttribs = new YAxisAttribs(20, getString(R.string.amount));		
		lineChart.initChartBackground(xAxisAttribs, yAxisAttribs);
		
		List<ChartDataSeries<LineChartData>> lineChartDataSeriesList = createMockLineChartData(minXValue,maxXValue); 
		lineChart.setChartContent(lineChartDataSeriesList);				
		
		lineChart.drawChart();
    }
    
    private List<ChartDataSeries<LineChartData>> createMockLineChartData(int minXValue,int maxXValue){				    	    	
    	List<ChartDataSeries<LineChartData>> lineChartDataSeriesList = new ArrayList<ChartDataSeries<LineChartData>>();

    	int[] colors = new int[]{Color.RED,Color.GREEN,Color.BLUE};    	
    	for(int i=1;i<=3;i++){    		
    		ChartDataSeries<LineChartData> lineChartDataSeries = new ChartDataSeries<LineChartData>(getString(R.string.test) + i, colors[i-1]);	    		    		
    		for(int j=minXValue;j<=maxXValue;j++){
    			lineChartDataSeries.addChartData(new LineChartData(j+0.5F, MathUtil.createRandomValue(50*i-10,50*i+10)));
    		}
    		lineChartDataSeriesList.add(lineChartDataSeries);     		    		
    	}
    	
		return lineChartDataSeriesList;
	}    
}